/**
 * 福利页面
 */
class welfareSub extends eui.Component{
    private back:eui.Button;
    private list:eui.List;
    private sign:eui.Image;
    private _calendar:Array<any> = [];
    private _arrayData:eui.ArrayCollection;
    private _index:number = 0;
    private _isSign:number = 0;//0:没有签到，1：签到完成
    //
    private today7:eui.Image;
    private isSign7:eui.Image;
    private title:eui.Label;
    private award:eui.Image;
    private today7label:eui.Label;
    public constructor(){
        super();
       // this.cacheAsBitmap =true;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "welfareSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.back.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.dispatchEventWith("return");
        },this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this._isSign == 1){
                let toast = new Toast();
                toast.show("今日已签到");
                this.addChild(toast);
            }else{
                let dataPost = {"nickname":common.userInfo.nickName};
                socket.request("sign",dataPost);
            }
        },this);

        Dispatcher.ins.addEventListener("signList",this.assignValue,this);
        Dispatcher.ins.addEventListener("sign",this.success,this);

        this._arrayData =  new eui.ArrayCollection(this._calendar);
        this.list.dataProvider = this._arrayData;
        this.list.itemRenderer = welfareIR;
        socket.request("signList");
    }

    public assignValue(evt:GameEvent){
        let obj = evt.data;
        let list = obj.list;
        let index = obj.time;
        let isSign = obj.isSign;
        this._isSign = isSign;
        if(isSign == 0){
            this._index = index + 1;
        }else{
            this._index = index;
        }
        for(let i = 0;i < 6;i++){
            let trObj:any;
            trObj = list[i];
            trObj.title = trObj.AwardName + "X" + trObj.AwardNum;
            if(trObj.Ctype == 0){
                trObj.icon = "icon_welfare_sun_png";
            }else{
                trObj.icon = "harvest_"+ trObj.AwardId+"_png";
            }
            if(i + 1 < this._index){
                trObj.today = false;
                trObj.node = "已领取";
                trObj.isSign = 1;
            }else if(i + 1 == this._index){
                trObj.today = true;
                if(isSign == 1){
                    trObj.node = "已领取";
                    trObj.isSign = 1;
                }else{
                    trObj.node = "点击领取";
                    trObj.isSign = 0;
                }
            }else{
                trObj.isSign = 0;
            }
            this._calendar.push(trObj);
        }
        
        let trObj = list[6];
        this.title.text = trObj.AwardName + "X" + trObj.AwardNum;
        if(trObj.Ctype == 0){
            trObj.icon = "icon_welfare_sun_png";
        }else{
            trObj.icon = "harvest_"+ trObj.AwardId+"_png";
        }
        this.award.source = trObj.icon;
        if(this._index == 7){
            this.today7.visible = true;
            this.today7label.visible = true;
            if(isSign == 1){
                this.today7label.text = "已领取";
                this.isSign7.visible = true;
            }else{
                this.today7label.text = "点击领取";
            }
        }
        this._arrayData =  new eui.ArrayCollection(this._calendar);
        this.list.dataProvider = this._arrayData;
        this._arrayData.refresh();
    }
    public success():void{
        this._isSign = 1;
        if(this._index !== 7){
            this._calendar[this._index - 1].node = "已领取";
            this._calendar[this._index - 1].isSign = 1;
            this._arrayData = new eui.ArrayCollection(this._calendar);
            this.list.dataProvider = this._arrayData;
            this._arrayData.refresh();
        }else{
            this.isSign7.visible = true;
            this.today7label.text = "已领取";
        }

    }
}
class welfareIR extends eui.ItemRenderer{
    private today:eui.Image;
    private isSign:eui.Image;
    private node:eui.Label;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "welfareIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
    }
    protected dataChanged():void{
        if(this.data.today){
            this.today.visible = true;
        }
        if(this.data.isSign == 1){
            this.isSign.visible = true;
            this.node.stroke = 1;
            this.node.strokeColor = 0xAAB1C4;
        }else{
            this.isSign.visible = false;
        }
    }
}